/*
 * EliminarVaca.java
 *
 * Created on 13-dic-2011, 20:55:10
 */

package interfazgrafica.modificar.vacas;

import interfazgrafica.ayuda.VentanaAyuda;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lauaa
 */
public class EliminarVaca extends javax.swing.JDialog {
    private final Connection conn;
    private final Statement stmt;
    private static String[] args;

    /** Creates new form EliminarVaca */
    public EliminarVaca(java.awt.Frame parent, boolean modal) throws ClassNotFoundException, SQLException {
        super(parent, modal);
        initComponents();
        //Accedo a la base de datos.
            Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "system", "terrano89");
            stmt = conn.createStatement();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelNumero = new javax.swing.JLabel();
        jTextFieldNumero = new javax.swing.JTextField();
        jButtonEliminarNumero = new javax.swing.JButton();
        jButtonAyuda = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cowmanager.CowManagerApp.class).getContext().getResourceMap(EliminarVaca.class);
        jLabelNumero.setText(resourceMap.getString("jLabelNumero.text")); // NOI18N
        jLabelNumero.setName("jLabelNumero"); // NOI18N

        jTextFieldNumero.setText(resourceMap.getString("jTextFieldNumero.text")); // NOI18N
        jTextFieldNumero.setName("jTextFieldNumero"); // NOI18N

        jButtonEliminarNumero.setText(resourceMap.getString("jButtonEliminarNumero.text")); // NOI18N
        jButtonEliminarNumero.setName("jButtonEliminarNumero"); // NOI18N
        jButtonEliminarNumero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarNumeroActionPerformed(evt);
            }
        });

        jButtonAyuda.setText(resourceMap.getString("jButtonAyuda.text")); // NOI18N
        jButtonAyuda.setName("jButtonAyuda"); // NOI18N
        jButtonAyuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAyudaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(327, Short.MAX_VALUE)
                .addComponent(jButtonAyuda)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jLabelNumero)
                .addGap(18, 18, 18)
                .addComponent(jTextFieldNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(jButtonEliminarNumero)
                .addContainerGap(146, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(89, 89, 89)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNumero)
                    .addComponent(jTextFieldNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonEliminarNumero))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                .addComponent(jButtonAyuda)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonEliminarNumeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarNumeroActionPerformed
        String consulta = "DELETE FROM Vacas WHERE (numeroenexplotacion = '";
        consulta = consulta + jTextFieldNumero.getText() + "')";
        //Hago una consulta y la guardo en rs.
	ResultSet rs = null;
        try {
            rs = stmt.executeQuery(consulta);
        } catch (SQLException ex) {
            Logger.getLogger(AñadirVaca.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(AñadirVaca.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(AñadirVaca.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(AñadirVaca.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.dispose();
        ModificarVacas.main(args);
    }//GEN-LAST:event_jButtonEliminarNumeroActionPerformed

    private void jButtonAyudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAyudaActionPerformed
        VentanaAyuda ayuda = new VentanaAyuda("Modificar vacas");
        ayuda.setVisible(true);
    }//GEN-LAST:event_jButtonAyudaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    EliminarVaca dialog = new EliminarVaca(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        public void windowClosing(java.awt.event.WindowEvent e) {
                            ModificarVacas.main(args);
                        }
                    });
                    dialog.setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(EliminarVaca.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(EliminarVaca.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAyuda;
    private javax.swing.JButton jButtonEliminarNumero;
    private javax.swing.JLabel jLabelNumero;
    private javax.swing.JTextField jTextFieldNumero;
    // End of variables declaration//GEN-END:variables

}
